package com.zlx.sharelive;

import android.content.Intent;
import android.os.Handler;

import com.zlx.module_base.BaseApplication;
import com.zlx.module_base.base_util.SPUtil;
import com.zlx.module_base.config.ModuleLifecycleConfig;
import com.zlx.sharelive.activity.LoginAc;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import io.rong.imkit.RongIM;


/**
 * @date: 2019\3\8 0008
 * @author: zlx
 * @email: 1170762202@qq.com
 * @description:
 */
public class MyApp extends BaseApplication {

    private static final String TAG = MyApp.class.getSimpleName();

    private static int mMainThreadId;
    private static Thread mMainThread;
    private static Handler mMainHandler;
    private static MyApp instance = null;
    public static String mToken;


    public static MyApp getInstance() {
        if (instance == null) {
            instance = new MyApp();
        }
        return instance;
    }


    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;

        String appKey = "x18ywvqfxn5yc";
        RongIM.init(this.getApplicationContext(), appKey);

        ModuleLifecycleConfig.getInstance().initModuleAhead(this);
        EventBus.getDefault().register(this);

        mMainThreadId = android.os.Process.myTid();
        mMainThread = Thread.currentThread();
        mMainHandler = new Handler();
    }


    /**
     * 获取主线程ID
     */
    public static int getMainThreadId() {
        return mMainThreadId;
    }

    public static Thread getMainThread() {
        return mMainThread;
    }

    public static Handler getMainHandler() {
        return mMainHandler;
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(Integer event) {
        System.out.println("收到消息" + event.toString());
    }


    public void backToLogin() {
        // 清除用户令牌
        RongIM.getInstance().disconnect();
        RongIM.getInstance().logout();
        SPUtil.setObjectToShare(instance, null, Constants.USERINFO);
        SPUtil.put(instance, Constants.SP_TOKEN, "");
        SPUtil.put(instance, Constants.SP_ID, "");
        SPUtil.put(instance, Constants.SP_IM_TOKEN, "");
        SPUtil.put(instance, Constants.SP_SAVE_PHONE, "");
        SPUtil.put(instance, Constants.SP_VIP, "");
        SPUtil.put(instance, Constants.SP_CITY, "");
        // 返回登录页
        Intent intent = new Intent(instance, LoginAc.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        instance.startActivity(intent);
    }
}
